﻿Imports DTO
Imports System.Data.SqlClient

Public Class KhachHangDAO
    Public Shared Function LayDSKhachHang() As DataTable
        Return CKetNoi.GetData("LayDSKhachHang")
    End Function

    Public Shared Function ThemKhachHang(kh As KhachHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@tenKH", kh.TenKhachHang))
        lst.Add(New SqlParameter("@maKV", kh.MaKhuVuc))
        lst.Add(New SqlParameter("@dc", kh.DiaChi))
        lst.Add(New SqlParameter("@mst", kh.MaSoThue))
        lst.Add(New SqlParameter("@dt", kh.DienThoai))
        lst.Add(New SqlParameter("@tk", kh.TaiKhoan))
        lst.Add(New SqlParameter("@fax", kh.Fax))
        lst.Add(New SqlParameter("@dd", kh.DiDong))
        lst.Add(New SqlParameter("@web", kh.TrangWEB))
        lst.Add(New SqlParameter("@nganhang", kh.NganHang))
        lst.Add(New SqlParameter("@email", kh.Email))
        lst.Add(New SqlParameter("@ghno", kh.GioiHanNo))
        lst.Add(New SqlParameter("@noht", kh.NoHienTai))
        lst.Add(New SqlParameter("@ck", kh.ChiecKhau))
        lst.Add(New SqlParameter("@yah", kh.NickYahoo))
        lst.Add(New SqlParameter("@sky", kh.NickSkype))
        lst.Add(New SqlParameter("@nguoilh", kh.NguoiLienHe))
        lst.Add(New SqlParameter("@maTT", kh.MaTrangThai))

        kt = CKetNoi.ExecQueryStoreProc("ThemKhachHang", lst)
        Return kt
    End Function

    Public Shared Function CapNhatKhachHang(kh As KhachHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@maKH", kh.MaKhachHang))
        lst.Add(New SqlParameter("@tenKH", kh.TenKhachHang))
        lst.Add(New SqlParameter("@maKV", kh.MaKhuVuc))
        lst.Add(New SqlParameter("@dc", kh.DiaChi))
        lst.Add(New SqlParameter("@mst", kh.MaSoThue))
        lst.Add(New SqlParameter("@dt", kh.DienThoai))
        lst.Add(New SqlParameter("@tk", kh.TaiKhoan))
        lst.Add(New SqlParameter("@fax", kh.Fax))
        lst.Add(New SqlParameter("@dd", kh.DiDong))
        lst.Add(New SqlParameter("@web", kh.TrangWEB))
        lst.Add(New SqlParameter("@nganhang", kh.NganHang))
        lst.Add(New SqlParameter("@email", kh.Email))
        lst.Add(New SqlParameter("@ghno", kh.GioiHanNo))
        lst.Add(New SqlParameter("@noht", kh.NoHienTai))
        lst.Add(New SqlParameter("@ck", kh.ChiecKhau))
        lst.Add(New SqlParameter("@yah", kh.NickYahoo))
        lst.Add(New SqlParameter("@sky", kh.NickSkype))
        lst.Add(New SqlParameter("@nguoilh", kh.NguoiLienHe))
        lst.Add(New SqlParameter("@maTT", kh.MaTrangThai))

        kt = CKetNoi.ExecQueryStoreProc("CapNhatKhachHang", lst)
        Return kt
    End Function

    Shared Function layDanhSachKHTenVaMa() As List(Of KhachHangDTO)
        Dim cn As SqlConnection = CKetNoi.Connect
        Dim dsKH As New List(Of KhachHangDTO)
        Try
            Dim cmd As New SqlCommand
            cmd.CommandText = "usp_LayDsKhachHang"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cn
            cn.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader
            While reader.Read
                Dim kh As New KhachHangDTO
                kh.MaKhachHang = reader("MaKhachHang")
                kh.TenKhachHang = reader("TenKhachHang")
                kh.DiaChi = reader("DiaChi")
                kh.DienThoai = reader("DienThoai")
                kh.ChiecKhau = reader("ChiecKhau")
                dsKH.Add(kh)
            End While
            cn.Close()
        Catch ex As Exception
            cn.Close
        End Try


        Return dsKH
    End Function

    Public Shared Function XoaKhachHang(kh As KhachHangDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@maKH", kh.MaKhachHang))
        kt = CKetNoi.ExecQueryStoreProc("XoaKhachHang", lst)
        Return kt
    End Function

End Class
